<?php

/**
 * sfSWFUpload actions.
 *
 * @package    sf_sandbox
 * @subpackage sfSWFUpload
 * @author     Mauro Casula
 */
class sfSWFUploadActions extends sfActions
{

  public function executeIndex() {
	$this->redirect("sfSWFUpload/fileSizeMultiUpload");
  }
	
  public function executeExample() {
		
  }
	
  public function executeShowUploads() {
 	    $this->getPhotoUploads();
  }
	
  public function executeSimpleMultiUpload() {
  	
  	 $this->getPhotoUploads();
  }
  
  public function executeFileSizeMultiUpload() {
  	
  	 $this->getPhotoUploads();
  }
 
  public function executeUpload() {
	
    	$upload_name = "Filedata";
	    $temp_file = $this->getRequest()->getFilePath($upload_name);
		
	    $path_info = pathinfo($_FILES[$upload_name]['name']);
		$ext = ".".$path_info["extension"];
		
		$fileName = md5($temp_file.time()); 
	
		$this->fileUpload($fileName, $temp_file, $ext);

  } 
  
 

 
  protected function getPhotoUploads() {
   		$foto_gallery_dir = "/".sfConfig::get("app_swfupload_gallery_folder")."/";
        $small_gallery_dir = $foto_gallery_dir."/".sfConfig::get("app_swfupload_thumbs_folder");
	    $big_gallery_dir = $foto_gallery_dir."/".sfConfig::get("app_swfupload_images_folder");
	  	$this->photos = array();
	  	
	  	if (is_dir(sfConfig::get('sf_web_dir').$small_gallery_dir)) {

		    if ($gd = opendir(sfConfig::get('sf_web_dir').$small_gallery_dir)) {
		    		
		        while (($file = readdir($gd)) !== false) {
		        	  	
		        	if (
		        	(filetype(sfConfig::get('sf_web_dir').$small_gallery_dir ."/". $file) != "dir") 
		         && ($file != ".") 
		         && ($file != "..")
		         && ($file != "Thumbs.db") ) {
		        			$this->photos[] = $file;
		        	}
		        }
		    }
	
	  	} 
	  	else {
	  		$this->photos[] = null;
	  	}
	  	
	  	$this->big_gallery_dir = $big_gallery_dir;
	 	$this->small_gallery_dir = $small_gallery_dir;
  }
  
 
  
  protected function fileUpload($fileName, $temp_file, $ext) { 

  	  	  // Save Thumb and foto in directory
		  // CREATE THE DIRECTORIES IF DOESNT EXIST
		  $foto_gallery_root_dir = sfConfig::get('sf_web_dir')."/".sfConfig::get("app_swfupload_gallery_folder")."/";
		  $foto_gallery_dir = $foto_gallery_root_dir."";
		  			
		  if ( !is_dir($foto_gallery_root_dir)) {
		  	mkdir($foto_gallery_root_dir);
		  }
		  
  		  if ( !is_dir($foto_gallery_dir)) {
			mkdir($foto_gallery_dir);
		  }
				
          if (sfConfig::get("app_swfupload_create_thumb") == "true") {
            $small_gallery_dir = $foto_gallery_dir."/".sfConfig::get("app_swfupload_thumbs_folder");	
            if ( !is_dir($small_gallery_dir)) {
			 mkdir($small_gallery_dir);
		    }
          	
	      	// Create the thumbnail
		    $thumbnail = new sfThumbnail(sfConfig::get("app_swfupload_thumb_width"), sfConfig::get("app_swfupload_thumb_height"),true,true,sfConfig::get("app_swfupload_thumb_quality"));
	      	$thumbnail->loadFile($temp_file);
	      	
	      	$thumbnail->save($small_gallery_dir."/".$fileName.$ext, sfConfig::get("app_swfupload_image_mime"));
	      }
			
	      $big_gallery_dir = $foto_gallery_dir."/".sfConfig::get("app_swfupload_images_folder");
  		  if ( !is_dir($big_gallery_dir)) {
			mkdir($big_gallery_dir);
		  }
		  
		  $foto = new sfThumbnail(sfConfig::get("app_swfupload_image_width"), sfConfig::get("app_swfupload_image_height"),true,true,sfConfig::get("app_swfupload_image_quality"));
		  $foto->loadFile($temp_file);
		  
		  $foto->save($big_gallery_dir."/".$fileName.$ext, sfConfig::get("app_swfupload_image_mime"));

		  unlink($temp_file);

  }
}
